$(function() {
    var id = tools.queryString().id || 1;
    //获取新闻详情
    function getNewsDetail(id) {
        $.ajax({
            type: "get",
            url: "http://127.0.0.1:3000/getnewsdetail",
            data: {
                id: id
            },
            success: function(data) {
                $(".writeComment").html(template("msgAndClick", { msgAndClick: data.data.data }));
                if (data.data.data.type === 1) {
                    $(".newsArticle").removeClass("hideNewsArticle");
                    $(".newsArticle").html(template("newsArticleText", { newsArticlMsg: data.data.data }));
                } else if (data.data.data.type === 2) {
                    $($(".newItem")[0]).css({ "borderTop": "none" });
                    $(".videoContainer").removeClass("hideVideoContainer");
                    $(".videoContainer").html(template("videoMsg", { videoMsg: data.data.data }));
                    $(".recommendNews").html(template("recommendNewsTpl", { newsList: data.data.data.list }));
                }
            }
        });
    }
    getNewsDetail(id);
    //推荐新闻点击跳转
    $(".recommendNews").on("click", ".newItem", function() {
        var id = $(this).attr("newsId");
        location.href = location.href.slice(0, -1) + id;
    });
    var pageNum = 1;
    window.total = 0;
    window.commentList = [];
    //新闻评论
    function getNewsComment(id, pageNum, commentList) {
        $.ajax({
            type: "get",
            url: "http://127.0.0.1:3000/getnewscomments",
            data: {
                id: id,
                pageNum: pageNum
            },
            success: function(data) {
                window.commentList = commentList.concat(data.data.data.list);
                window.total = data.data.data.total;
                $(".commentsContainer").html(template("userComment", { commentList: window.commentList }));
            }
        });
    }
    getNewsComment(id, pageNum, window.commentList);
    $(".moreCommentsBtn").on("click", function() {
        if (window.total > 1) {
            pageNum++;
            getNewsComment(id, pageNum, window.commentList);
        } else {
            $(this).html("没有更多评论了");
            return;
        }
    })
});